define(['jquery','mylayer','asyncForm','post_item'], function ($,mylayer,asyncForm,post_item) {
	var app={
		//初始化事件
		init:function(){
			var _this=this;
			$.postAjax("/post/updateView",{'id':$("#post_detail_id").val()},function(){});
			//选择表情
			$(".emoji_select").off("click").on("click",function(){
				var emoji_list=$(".emoji_list");
				if(emoji_list.css("display")=="none"){
					emoji_list.show();
					emoji_list.load("/statichtml/emoji.php",function(){
						//选择表情包
						$(".emoji_list img").off("click").on("click",function(){
							var imgsrc=$(this).attr("src");
							var review_content=$(".post_review_content");
							if(review_content.hasClass('contenteditable')){
								var content=$.trim(review_content.html());
								review_content.html(content+'<img src="'+imgsrc+'" />');
								emoji_list.hide();
							}
						});
					});
				}
				else{
					emoji_list.hide();
				}
			});
			$(".review_submit").off("click").on("click",function(){
				var review_content=$(".post_review_content");
				var content=$.trim(review_content.html());
				if(content){
					if($(this).hasClass("disabled")){
						return false;
					}
					$(this).addClass("disabled");
					review_content.next(".error_msg").hide();
					var id=$(this).attr("data-id");
					var that=this;
					$.postAjax("/review/add",{'id':id,'content':content},function(result){
						$(that).removeClass("disabled");
						if(result.code=="0x00001"){
							md_loginReg.showLogin();//显示登录
							//设置登录完成事件
							md_loginReg.initSuccess(function(){
								$(that)[0].click();
							});
							return false;
						}
						else if(result.code=="0x00000"){
							var data=[];
							data.push(result.data);
							var html=_this.getReviewInfo(data);
							$(".post_review_lists").append(html);
						}
					},function(){
						$(that).removeClass("disabled");
					});
				}
				else{
					review_content.next(".error_msg").show();
				}
			});
			$(".post_detail .post_wish").off("click").on("click",function(){
				post_item.wish.call(this);//wish
			});
			$(".post_detail .interact").off("click").on("click",function(){
				post_item.interact.call(this);//点赞
			});
			//滚动加载
			if($(".detail_box").size()>0){
				var scroll_obj=$(window);
			}
			else{
				var scroll_obj=$(".layer_post_detail");
			}
			scroll_obj.off("scroll").on("scroll",function(event) {
				var html=$(".post_review_lists").html();
				var that=this;
				if($(".post_review_lists").hasClass("load")||$(".post_review_lists").hasClass("ajax_complete")){
					return false;
				}
				else{
					if($(this).hasClass('layer_post_detail')){
						var bottom_flag=$(this)[0].scrollTop >= ($(this)[0].scrollHeight - $(this).height()-100)?true:false;
					}
					else{
						var top=$('.post_review_lists').offset().top;
						var height=$('.post_review_lists').height();
						var bottom_flag=top+height<=md_common.getScrollTop()+md_common.getWindowHeight()?true:false;
					}
					if(bottom_flag){
						$(".post_review_lists").addClass("load");
						var postid=$("#post_detail_id").val();
						var offset=$(".post_review_lists>.post_review_list").size();
						$.postAjax("/review/getInfo",{'postid':postid,'offset':offset},function(result){
							$(".post_review_lists").removeClass("load");
							if(result&&result.list.length>0){
								var html=_this.getReviewInfo(result.list);
								$(".post_review_lists").append(html);
								_this.listEvent();
							}
							else{
								$(".post_review_lists").addClass('ajax_complete');
								$(scroll_obj).off('scroll');
							}
						});
		      		} 
				}
			});
		},
		//拼接评论数据
		getReviewInfo:function(data){
			var length=data.length;
			var html="";
			var shareid=$("#post_detail_id").val();
			for (var i = 0; i<length;i++) {
				html+='<div class="post_review_list clearfix">'+
					'<div class="clearfix"><div class="img"><img src="'+data[i]['photo']+'" /></div>'+
					'<div class="info">'+
					'<div>'+
						'<a href="/u/'+data[i]['fullname']+'" class="user">'+data[i]['fullname']+'</a>'+
						'<span class="time">'+data[i]['time']+'</span>'+
					'</div>'+
					'<p>'+data[i]['content']+'</p>'+
					'</div></div>'+
					'</div>';
			}
			return html;
		}
	}
	return app;
});